

package com.cn.项目.飞机预约管理系统.Logining;

import com.cn.项目.utils.JdbcUtils;
import com.cn.项目.飞机预约管理系统.Maininterface.Fail;
import com.cn.项目.飞机预约管理系统.Maininterface.Success;
import com.cn.项目.飞机预约管理系统.UserFrame.UserMainFrame;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;

public class UserLogin extends JFrame {
    public UserLogin() {
        initComponents();
    }

    //登录监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        String id = textField1.getText();
        System.out.println(id);
        String pass = passwordField1.getText();
        try {
            conn = JdbcUtils.getConnection();
            String sql = "select *from users where id=? and password=?";
            st = conn.prepareStatement(sql);
            st.setString(1,id);
            st.setString(2,pass);
            rs= st.executeQuery();
            if(rs.next()){
                new Success();
                new UserMainFrame();
                this.dispose();
            }else {
                new Fail();
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st ,rs );

        }
    }


    //注册监听
    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
          new UserRegister();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        label4 = new JLabel();
        textField3 = new JTextField();
        passwordField1 = new JPasswordField();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        setVisible(true);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("用户登录");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 10f));
        label1.setForeground(new Color(0, 255, 102));
        contentPane.add(label1);
        label1.setBounds(135, 5, 170, 40);

        //---- label2 ----
        label2.setText("账号");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        contentPane.add(label2);
        label2.setBounds(30, 60, 70, 40);
        contentPane.add(textField1);
        textField1.setBounds(115, 65, 180, 35);

        //---- label3 ----
        label3.setText("密码：");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 6f));
        contentPane.add(label3);
        label3.setBounds(30, 125, 65, 30);

        //---- label4 ----
        label4.setText("姓名");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 6f));
        contentPane.add(label4);
        label4.setBounds(30, 185, 60, 30);
        contentPane.add(textField3);
        textField3.setBounds(115, 185, 180, 35);
        contentPane.add(passwordField1);
        passwordField1.setBounds(115, 130, 180, 35);

        //---- button1 ----
        button1.setText("登录");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 6f));
        button1.setForeground(new Color(0, 255, 51));
        button1.addActionListener(e -> button1ActionPerformed(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(80, 250), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("注册");
        button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 6f));
        button2.setForeground(new Color(0, 153, 153));
        button2.addActionListener(e -> button2ActionPerformed(e));
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(225, 250), button2.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(400, 335));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField3;
    private JPasswordField passwordField1;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
